package com.open.api.peekapi.controller;

import com.open.api.peekapi.bean.ApiItem;
import com.open.api.peekapi.bean.ApiItemData;
import com.open.api.peekapi.services.ApiItemDataServices;
import com.open.api.peekapi.services.ApiItemServices;
import com.open.api.peekapi.util.HelpMe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {

	private static Logger log = LoggerFactory.getLogger(IndexController.class);

	@Autowired
	ApiItemServices apiItemServices;
	@Autowired
	ApiItemDataServices apiItemDataServices;

	@RequestMapping("/")
	public String index(HttpServletRequest request,Model model,Integer dataStat){
		if (dataStat==null)dataStat=1;
		model.addAttribute("eleCount",dataStat);



		log.info("index 首页");

		List<ApiItem> firstLevelList = apiItemServices.firstLevelItem();

		if (HelpMe.isNotNull(firstLevelList)){
			ApiItem which = firstLevelList.get(dataStat - 1);

			List<ApiItem> secondLevelList = apiItemServices.secondLevelByFirstLevelId(which.getId());

			model.addAttribute("firstLevelList",firstLevelList);
			model.addAttribute("secondLevelList",secondLevelList);
			model.addAttribute("hasData",1);
			model.addAttribute("id",which.getId());
		}else {
			model.addAttribute("hasData",0);
		}


		return "index";
	}




	@RequestMapping("/data")
	@ResponseBody
	public List<ApiItemData> data(HttpServletRequest request, String id){


		List<ApiItemData> list = apiItemDataServices.data(id);


		return list;
	}




	@RequestMapping("/getOne")
	@ResponseBody
	public ApiItemData getOne(String id){


		ApiItemData one = apiItemDataServices.getOne(id);


		return one;
	}











}



















